package com.gragra.nounIdentification.tests;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.StringReader;
import org.junit.Before;
import org.junit.Test;
import cern.colt.Arrays;
import com.gragra.data.Corpus;
import com.gragra.nounIdentification.ConvergenceByIterations;
import com.gragra.nounIdentification.CountIdentifier;
import com.gragra.nounIdentification.ExtendingNounIdentifier;
import com.gragra.nounIdentification.IncreasingMaxWithMin;
import com.gragra.nounIdentification.LinearIncreasingMax;
import com.gragra.nounIdentification.TypesPerTokenScorer;
import com.gragra.sampling.sequentialStructure.ExtensionLevels;
import com.gragra.sampling.sequentialStructure.WordAndTagHolder;
import com.gragra.sampling.sequentialStructure.WordType;
import com.gragra.sequentiaDataGenerator.BaseGenerator;
import com.gragra.sequentiaDataGenerator.CaseLevelAssigningSegmenter;
import com.gragra.sequentiaDataGenerator.PunctuationSplitOffSegmenter;
/**
 * 
 * @author Christoph Teichmann
 * 31.07.2013
 */
public class TestCountIdentifier
{
	private Corpus<WordAndTagHolder> corp;
	/**
	 * 
	 */
	private ExtendingNounIdentifier ni;
	@Before
	public void setUp() throws Exception
	{
		StringReader sr = new StringReader("A|NONE|ContextTagLevel|0 Cat|1|ContextTagLevel|1 is|0|ContextTagLevel|2 on|NONE|ContextTagLevel|3 the|0|ContextTagLevel|0 mat|x there.|NONE\nA|g|ContextTagLevel|0 horse|NONE|ContextTagLevel|1 is|0 on|NONE the|0 grass|c there.|NONE\n" +
				"A|NONE|ContextTagLevel|0 stain|1|ContextTagLevel|1 is|0|ContextTagLevel|2 on|NONE|ContextTagLevel|3 the|0|ContextTagLevel|0 mat.|NONE\nA|g|ContextTagLevel|0 ball|NONE|ContextTagLevel|1 is|0 on|NONE the|0 grass.|NONE\n" +
				"1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42\n" +
				"1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42 1|NONE|ContextTagLevel|42\n"+
				"1|NONE|ContextTagLevel|11 2|NONE|ContextTagLevel|11 3|NONE|ContextTagLevel|11 4|NONE|ContextTagLevel|11 5|NONE|ContextTagLevel|11 6|NONE|ContextTagLevel|11 7|NONE|ContextTagLevel|11");
		BufferedReader br = new BufferedReader(sr);
		BaseGenerator bg = new BaseGenerator(new CaseLevelAssigningSegmenter(new PunctuationSplitOffSegmenter("NONE")));
		corp = bg.createCorpus(br);
		ni = new CountIdentifier(corp, 3, 0, 0, new TypesPerTokenScorer(), new ConvergenceByIterations(2), new IncreasingMaxWithMin(new LinearIncreasingMax(1, 1), 2, 6), 2);
		ni.identify();
	}
	@Test
	public void testAnnotateCorpusFrames()
	{
		ni.annotateCorpusFrames(corp);
		for(int i=0;i<corp.size()-1;++i)
		{
			WordAndTagHolder wth = corp.getEntry(i);
			for(int j=0;j<wth.getLength();++j)
			{assertFalse(wth.getExtensionInformation().getFiniteInformation(j, ExtensionLevels.WordTypeLevel)==WordType.Noun && wth.getExtensionInformation().getInfiniteInformation(j, ExtensionLevels.ContextTagLevel)!=1);}
		}
		WordAndTagHolder wth = corp.getEntry(6);
		for(int i=0;i<wth.getLength();++i)
		{assertTrue(wth.getExtensionInformation().getFiniteInformation(i, ExtensionLevels.WordTypeLevel)==WordType.Noun);}
	}
	@Test
	public void testEvaluate()
	{
		corp.getEntry(6).getExtensionInformation().addInformation(0, ExtensionLevels.WordTypeLevel, WordType.Noun);
		corp.getEntry(0).getExtensionInformation().addInformation(1, ExtensionLevels.WordTypeLevel, WordType.Noun);
		corp.getEntry(0).getExtensionInformation().addInformation(3, ExtensionLevels.WordTypeLevel, WordType.Noun);
		double[] d = ni.evaluate(corp, true);
		assertEquals(d[0],2.0/11.0,0.00001);
		assertEquals(d[1],2.0/3.0,0.00001);
		assertEquals(d[2],0.28571428571428575,0.00000000000000001);
		System.out.println(Arrays.toString(d));
	}
	@Test
	public void testAnnotateCorpusWords()
	{
		ni.annotateCorpusWords(corp);
		for(int i=0;i<corp.size()-1;++i)
		{
			WordAndTagHolder wth = corp.getEntry(i);
			for(int j=0;j<wth.getLength();++j)
			{assertFalse(wth.getExtensionInformation().getFiniteInformation(j, ExtensionLevels.WordTypeLevel)==WordType.Noun && wth.getExtensionInformation().getInfiniteInformation(j, ExtensionLevels.ContextTagLevel)!=1);}
		}
		WordAndTagHolder wth = corp.getEntry(6);
		for(int i=0;i<wth.getLength();++i)
		{assertTrue(wth.getExtensionInformation().getFiniteInformation(i, ExtensionLevels.WordTypeLevel)==WordType.Noun);}
	}
}